package com.example.admin.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.example.admin.mapper.BlogMapper;
import com.example.admin.mapper.CategoryMapper;
import com.example.admin.service.BlogService;
import com.example.xo.pojo.Blog;
import com.example.xo.pojo.Category;
import com.example.xo.vo.BlogVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.jdbc.Null;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public boolean add(Blog blog) {
        //雪花id
        String id = IdUtil.getSnowflakeNextIdStr();
        //判断Title是否重复
        Blog byTitleInDb = findBlogByTitle(blog.getTitle());
        if (byTitleInDb != null) {
            return false;
        }
        //新增时间 数据库设计阶段我们有指定当新增时间是null的时候，采用当前时间
        //blog.setCreated(LocalDateTime.now());
        //判断是否成功写入
        return blogMapper.insert(blog) == 1;

    }

    @Override
    public boolean deleteById(String id) {
        Blog blog = findBlogById(id);
        //设置删除时间
        blog.setDeleted(LocalDateTime.now());
        //res 受影响的行数
        LambdaQueryWrapper<Blog> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Blog::getBlogId,id);
        int res = blogMapper.update(blog,wrapper);
        return res == 0;

    }

    @Override
    public boolean update(Blog blog) {

        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNull(Blog::getDeleted);
        wrapper.eq(Blog::getBlogId, blog.getBlogId());
        //修改时间
        blog.setUpdated(LocalDateTime.now());
            //判断标题是否已经重复
        BlogVO blogInDb = findByTitle(blog.getTitle());
        //数据中的记录要排除自己
        if (blogInDb!= null&&!blog.getBlogId().equals(blogInDb.getBlogId())){
            return false;
        }
        //去掉对作者修改
        blog.setBlogId(null);

        int ser = blogMapper.update(blog, wrapper);
        return ser > 0;
    }

    @Override
    public List<BlogVO> findAll() {
        List<BlogVO> vos=new ArrayList<>();
        //查询blog
        LambdaQueryWrapper<Blog> wrapper=new LambdaQueryWrapper<>();
        wrapper.isNull(Blog::getDeleted);
        wrapper.orderByDesc(Blog::getCreated);
        List<Blog> blogs = blogMapper.selectList(wrapper);
        //根据blog的categoryId来获取分类名
        for (Blog blog : blogs) {
            //通过分类id获取分类对象
            //todo 这段代码可以提取为一个方法
            Category category = categoryMapper.selectById(blog.getCategoryId());
            //组装vo
            BlogVO vo=new BlogVO();
            vo.setBlogId(blog.getBlogId());
            vo.setAuthor(blog.getAuthor());
            vo.setCategoryName(category.getCategoryName());
            vo.setTitle(blog.getTitle());
            vo.setCreated(blog.getCreated());
            //加入到容器中
            vos.add(vo);
        }

        return vos;
    }
    /**
     * 通过分类id获取分类对象
     *
     * @param blog
     * @return vo
     */
    @NotNull
    private BlogVO getBlogVO(Blog blog) {
        Category category = categoryMapper.selectById(blog.getCategoryId());
        //组装vo
        BlogVO vo = new BlogVO();
        vo.setBlogId(blog.getBlogId());
        vo.setAuthor(blog.getAuthor());
        vo.setCategoryName(category.getCategoryName());
        vo.setTitle(blog.getTitle());
        vo.setCreated(blog.getCreated());
        return vo;
    }

    @Override
    public BlogVO findById(String id) {
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNull(Blog::getDeleted);
        wrapper.eq(Blog::getBlogId, id);
        Blog blog = blogMapper.selectOne(wrapper);

        if (blog == null) {
            return null;
        }
        //通过blog中的categoryId查找
        BlogVO vo = getBlogVO(blog);
        return vo;
    }

    @Override
    public Blog findBlogById(String id) {
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNull(Blog::getDeleted);
        wrapper.eq(Blog::getBlogId, id);
        Blog blog = blogMapper.selectOne(wrapper);

        if (blog == null) {
            return null;

        }
        return blog;
    }

    @Override
    public BlogVO findByTitle(String title) {
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNull(Blog::getDeleted);
        wrapper.eq(Blog::getTitle, title);
        Blog blog = blogMapper.selectOne(wrapper);

        if (blog == null) {
            return null;
        }
        //通过blog中的categoryId查找
        BlogVO vo = getBlogVO(blog);

        return vo;
    }

    public Blog findBlogByTitle(String title) {
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNull(Blog::getDeleted);
        wrapper.eq(Blog::getTitle, title);
        Blog blog = blogMapper.selectOne(wrapper);

        if (blog == null) {
            return null;
        }
        return blog;
    }


    private boolean isTitleExist(Blog blog) {

        Blog blogbInDb = this.findBlogByTitle(blog.getTitle());
        if (blogbInDb != null) {
            log.info("分类{}重复", blog.getTitle());
            return true;
        }
        return false;
    }
}
